package com.dgut.sales.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dgut.sales.bean.Bills;
import com.dgut.sales.bean.Contract;
import com.dgut.sales.bean.Goods;
import com.dgut.sales.bean.Purchase;
import com.dgut.sales.dynamic.sql.bean.GoodsSearch;
import com.dgut.sales.service.BillsService;
import com.dgut.sales.service.ContractService;
import com.dgut.sales.service.DetailsService;
import com.dgut.sales.service.GoodsService;
import com.dgut.sales.service.PurchaseService;

@Controller
@RequestMapping("/goods")
public class GoodsController {
	
	@Autowired
	private GoodsService service;
	@Autowired
	private BillsService billsService;
	@Autowired
	private PurchaseService purchaseService;
	@Autowired
	private DetailsService detailsService;
	@Autowired
	private ContractService contractService;
	
	@ModelAttribute
	public void prepareModel(Model model) {
		model.addAttribute("goods", new Goods());
	}
	
	@GetMapping("/list")
	public String list(Model model) {
		List<Goods> goods = service.getAllGoods();
		model.addAttribute("goods", goods);
		return "/goods/goodsList";
	}
	
	@PostMapping("/inputGoods")
	public String inputGoods(Model model) {
		model.addAttribute("goods", new Goods());
		return "/goods/inputGoods";
	}
	
	@PostMapping("/addGoods")
	public String addGoods(@ModelAttribute Goods goods) {
		service.addGoods(goods);
		return "redirect:/goods/list";
	}
	
	
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, Model model) {
		Goods goods = service.getGoodsById(id);
		model.addAttribute("goods", goods);
		return"goods/editGoods";
	}
	
	@PutMapping("/update")
	public String update(@ModelAttribute Goods goods) {
		service.updateGoods(goods);
		return "redirect:/goods/list";
	}
	
	@GetMapping("/deleteGoods/{id}")
	public String deleteGoods(@PathVariable("id") Integer id) {
		service.deleteGoods(id);
		return "redirect:/goods/list";
	}
	@PostMapping("/inputCondition")
	public String inputCondition(Model model) {
		model.addAttribute("searchGoods", new GoodsSearch());
		return "goods/inputCondition";
	}
	
	
	@PostMapping("/search")
	public String search(@ModelAttribute GoodsSearch goodsSearch, Model model) {
		List<Goods> goods = service.search(goodsSearch);
		model.addAttribute("goods", goods);
		return "goods/searchResult";
	}
	
	@GetMapping("/putDown/{id}")
	public String putDown(@PathVariable("id") Integer id) {
		Goods goods = service.getGoodsById(id);
		if(goods.getStates().equals("上架")) {
			goods.setStates("下架");
			service.updateGoods(goods);
		}
		else {
			goods.setStates("上架");
			service.updateGoods(goods);
		}
		return "redirect:/goods/list";
	}
	
	@GetMapping("/billList")
	public String billList(Model model) {
		List<Bills> bills = billsService.getAllBills();
		model.addAttribute("bills", bills);
		return "bills/List";
	}

	@GetMapping("/deliver/{id}")
	public String deliver(@PathVariable Integer id) {
		billsService.updateStates(id);
		Bills bills = billsService.getBillsById(id);
		detailsService.updateStates(bills.getDetailId());
		Goods goods = service.getGoodsById(bills.getGoodsId());
		goods.setQuantity(goods.getQuantity()-bills.getNumber());
		service.updateGoods(goods);
		//未发货的计数器-1，以判断是否已全部发货
		Integer contractId = detailsService.getDetailsById(bills.getDetailId()).getContractId();
		Contract contract = contractService.getContractById(contractId);
		contract.setDetailsNum(contract.getDetailsNum()-1);
		contractService.updateContract(contract);
		if(contract.getDetailsNum()==0) {
			contract.setStates("履行完毕");
			contractService.updateContract(contract);
		}
		return "redirect:/goods/billList";
	}
	
	@PostMapping("/addpurchase/{contractId}")
	public String purchase(@ModelAttribute Purchase purchase, @PathVariable("contractId") Integer contractId) {
		purchaseService.addPurchase(purchase);
		return "redirect:/contract/getDetails/" + contractId;	
	}
	
	@GetMapping("/purchaseList")
	public String purchaseList(Model model) {
		List<Purchase> purchases = purchaseService.getAllPurchase();
		model.addAttribute("purchases", purchases);
		return "bills/purchaseList";
	}
	
	@GetMapping("/purchased/{id}")
	public String purchased(@PathVariable("id") Integer id) {
		purchaseService.updateStates(id);
		Purchase purchase = purchaseService.getPurchaseById(id);
		Goods goods = service.getGoodsById(purchase.getGoodsId());
		goods.setQuantity(goods.getQuantity()+purchase.getNumber());
		service.updateGoods(goods);
		return "redirect:/goods/purchaseList";
	}
	
	@GetMapping("/statistics")
	public String statistics(Model model) {
		List<Map<String, Object>> fromGoodsStatistics = service.searchGoodsWithMoney();
		model.addAttribute("fromGoodsStatistics", fromGoodsStatistics);
		return "statistics/fromGoods";
	}
	
}
